//
//  HDJiHuoNumberTableViewCell.swift
//  Wmsmobile
//
//  Created by wangshuning on 16/9/29.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


protocol HDJiHuoNumberTableViewCellDelegate: NSObjectProtocol {
    
    func finishjiHuoNumInput(_ cell: HDJiHuoNumberTableViewCell)
    func returnjiHuoNumInput(_ cell: HDJiHuoNumberTableViewCell)
}

class HDJiHuoNumberTableViewCell:HDFounctionBaseCell , UITextFieldDelegate ,HDInputViewStateProtocol {
    //MARK: - property
    weak var Jihuodelegate:HDJiHuoNumberTableViewCellDelegate?
    
    var inputState:inputViewState = inputViewState.activated
    
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
            switch inputState { //改变状态
            case .activated:
                self.isUserInteractionEnabled = true
                self.label3.becomeFirstResponder()
            case .disabled:
//                self.userInteractionEnabled = false
                self.label3.resignFirstResponder()
            case .wrong:
                self.isUserInteractionEnabled = true
                self.label3.becomeFirstResponder()
            }
        }
    }
    
    var dict:[String : String]?{
        didSet{
            label1.text = dict!["left"]
            label1.textColor = UIColor.hexStringToUIColor("#666666")
            label1.font = UIFont.systemFont(ofSize: self.getFont())
            label2.text = dict!["middle"]
            label2.textColor = UIColor.hexStringToUIColor("#666666")
            label2.font = UIFont.systemFont(ofSize: self.getFont())
            label3.text = dict!["right"]
            label3.delegate = self
            label3.textColor = UIColor.hexStringToUIColor("#666666")
            label3.font = UIFont.systemFont(ofSize: self.getFont())
        }
    }
    
    
    lazy  var label1:UILabel = {
        let label = UILabel()
//        label.backgroundColor = UIColor.redColor()
        return label
    }()
    lazy  var label2:UILabel = {
        let label = UILabel()
        return label
    }()
    lazy  var label3:UITextField = {
        let label = UITextField()
        label.placeholder = "请输入"
        label.textAlignment = .right
        label.keyboardType = UIKeyboardType.numbersAndPunctuation
//        label.backgroundColor = UIColor.redColor()
        return label
    }()
    
    
    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "NumberInputCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "NumberInputCell")
        self.setUpUI()
    }
    //MARK: - public
    override func clearContent() {
        self.label3.text = ""
        self.label1.text = ""
    }
    //MARK: - private
    func setUpUI(){
//        self.label3.keyboardType = .NumbersAndPunctuation
//        userName.returnKeyType = UIReturnKeyType.Next
//        self.label3.returnKeyType = .Go
        NotificationCenter.default.addObserver(self, selector: #selector(HDJiHuoNumberTableViewCell.textfieldDidChanged(_:)), name: NSNotification.Name.UITextFieldTextDidEndEditing, object: self.label3)
        
        
        self.contentView.addSubview(self.label3)
        self.contentView.addSubview(self.label1)
        self.contentView.addSubview(self.label2)
        label1.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(18)
            make.centerY.equalTo(self.contentView)
            //            make.width.equalTo(80)
        }
        label2.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.contentView)
            make.centerX.equalTo(self.contentView)
        }
        label3.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.contentView)
            make.trailing.equalTo(self.contentView).offset(-28)
            make.width.equalTo(100)
            make.height.equalTo(44)
        }
        
    }
    
    
    //MARK: - delegate
    func isEmpty() -> (Bool) {
        return self.label3.text?.characters.count > 0 ? false : true
    }
    
    func finishInput(_ text: String?) {
        
    }
    
    //MARK: - UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        if (label3.text!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 4 ){
//            
////            let alert = UIAlertView(title: "温馨提示: ",
////                                    message: "您输入的数据有误，请重新输入",
////                                    delegate: self,
////                                    cancelButtonTitle: "确定")
//            label3.text = ""
//            return false
//        }
//        return true
        
        var futureString = textField.text ?? ""
        futureString.insert(contentsOf: string.characters, at: futureString.characters.index(futureString.startIndex, offsetBy: range.location))
        if futureString.characters.count > 4 {
            return false
        }
        return numFilter(0, string: futureString)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//        self.label3.resignFirstResponder()
        if self.Jihuodelegate != nil {
            self.Jihuodelegate?.returnjiHuoNumInput(self)
        }
            return true
    }
    func textfieldDidChanged(_ notification: Notification){
        if self.Jihuodelegate != nil {
            self.Jihuodelegate?.finishjiHuoNumInput(self)
        }
    }
}
